﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class TrainSchemeList : System.Web.UI.Page
    {
        protected EmpBasicInfo ebi;
        protected void Page_Load(object sender, EventArgs e)
        {
            ebi = Session["ebi"] as EmpBasicInfo;
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                var v = ctx.EmpDepPositions.Where(r => r.EmpId == ebi.Id && r.PosId <= 3);
                var v2 = from r in ctx.EmpRoles where r.EmpId == ebi.Id join r2 in ctx.RoleControllerDepts on r.RoleId equals r2.RoleId where r2.DeptId == null && (r2.Controller == "TrainSchemes" || r2.Controller == null) select 0;
                string strSql;
                if (v.Count() > 0 || v2.Count() > 0 || ebi.EmpRoles.Where(r => r.RoleId == 1).Any())
                {
                    strSql = @"with t as (select Id,Pid,cast(Text as nvarchar(500)) Text,OrderNo,1 lvl,cast(Id as nvarchar(50)) orderStr from Depts where Pid is null
union all
select d.Id,d.Pid,cast(REPLICATE('--',t.lvl)+d.Text as nvarchar(500)),
d.OrderNo,t.lvl+1,cast(t.orderStr+cast(d.Id as nvarchar(50)) as nvarchar(50))
from Depts d,t where d.Pid=t.id and d.IsValid=1
)
  select Id,Text from t order by orderStr;";
                }
                else
                {
                    strSql = string.Format(@"with t as(
SELECT b.Id,b.Text FROM EmpDepPositions a,Depts b where a.DeptId=b.Id and a.PosId<=7 and a.EmpId={0}
union all
select isnull(c.Id,1),isnull(c.Text,'太阳神直销总部') from EmpRoles a
	inner join RoleControllerDepts b on a.RoleId=b.RoleId and a.EmpId={0} and (b.Controller='{1}' or b.Controller is null)
	left join Depts c on b.DeptId=c.Id
union all
select a.Id,a.Text from Depts a,t where a.Pid=t.Id and a.IsValid=1
)
select distinct Id,Text from t;", ebi.Id, "TrainSchemes");
                }
                var depts = ctx.ExecuteQuery<DeptDTO>(strSql);
                Dept.DataSource = depts;
                Dept.DataValueField = "Id";
                Dept.DataTextField = "Text";
                Dept.DataBind();

                _getDatas();
            }
        }

        protected void btnSeach_Click(object sender, EventArgs e)
        {
            _getDatas();
        }

        private void _getDatas()
        {
            string strSql = string.Format(@"with t as(select Id,Text from Depts where Id={0}
union all
select a.Id,a.Text from Depts a,t where a.Pid=t.Id),
t2 as(select DeptName,EmpId,MAX(isnull(UpdateDate,CreatDate)) LastUpdateDate from TrainSchemes
  group by DeptName,EmpId)
  select t2.*,c.Name from t,t2,Employee c
  where t.Text=t2.DeptName and t2.EmpId=c.Id
  order by t2.LastUpdateDate desc;", Dept.Value);
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            var list = ctx.ExecuteQuery<TrainNeedDTO>(strSql).ToList();
            if (ebi.EmpDepPos.Select(r => r.PosId <= 7).Count() > 0 || ebi.EmpDepPos.FirstOrDefault().DeptId == 9 || ebi.EmpRoles.Where(r => r.RoleId == 1).Any())
            {
                rptList.DataSource = list;
                rptList.DataBind();
            }
            else
            {
                Response.Redirect("TrainSchemeInfo.aspx");
            }
            if (ebi.EmpRoles.Where(r => r.RoleId == 1).Any())
            {
                btnNew.Visible = true;
            }
        }
    }
}